<template>
  <div class="temp-report">
    <div>
      <label>{{ $t('route.DeviceManager') }}</label>
      <div></div>
      <button><img /><label>{{ $t('route.Temperature_report') }}</label></button>
    </div>

    <div>
      <el-select size="default" v-model="query.workshop" @change='handlePageChange(1)'
        :placeholder="$t('route.Please_select_workshop')">
        <el-option v-for="item in workshopList" :key="item" :label="item" :value="item" />
      </el-select>
      <el-button type="primary" class="mgr-left-10" @click="isShowDrawer = true">{{ $t('route.More_Filters')
      }}</el-button>
      <div class="div-handle-right">
        <el-button type="danger" class="mgr-left-10" @click="deleteClick">{{ $t('route.Delete') }}</el-button>
        <el-button type="success" class="mgr-left-10" @click="editClick">{{ $t('route.Editing') }}</el-button>
        <el-button type="primary" class="mgr-left-10" @click="extClick">{{ $t('route.export') }}</el-button>
      </div>
    </div>

    <div>
      <el-table @selection-change="selectionChange" :data="tableData" :row-style="{ height: '50px' }">
        <el-table-column type="selection" width="55" />
        <el-table-column :formatter="format1" :label="$t('route.date')" />
        <el-table-column :formatter="format2" :label="$t('route.time')" />
        <el-table-column prop="workshop" :label="$t('route.Workshop')" />
        <el-table-column prop="proLine" :label="$t('route.Production_line')" />
        <el-table-column :formatter="format3" :label="$t('route.Machine_number')" width="200" />
        <el-table-column :formatter="format4" :label="$t('route.kind')" />
        <el-table-column prop="stdTemp" :label="$t('route.Standard_temperature')" />
        <el-table-column :formatter="format5" prop="temp1" :label="$t('route.Left')" />
        <el-table-column :formatter="format5" prop="temp2" :label="$t('route.Medium')" />
        <el-table-column :formatter="format5" prop="temp3" :label="$t('route.Right')" /> -->
      </el-table>
      <div>
        <el-pagination background @current-change="handlePageChange" :current-page="query.pageNum"
          :page-size="query.pageSize" layout="total, prev, pager, next" :total="query.total"></el-pagination>
      </div>
    </div>


    <el-drawer v-model="isShowDrawer">
      <div class="drawer">
        <div>
          <div>{{ $t('Date_selection') }}</div>
          <el-radio-group @change='dateTypeChange' v-model="drawerValue.dateType" size="large">
            <el-radio-button :label="$t('route.today')" />
            <el-radio-button :label="$t('route.yesterday')" />
            <el-radio-button :label="$t('route.This_week')" />
            <el-radio-button :label="$t('route.This_month')" />
            <el-radio-button :label="$t('route.Last_month')" />
            <el-radio-button :label="$t('route.Range')" />
          </el-radio-group>
          <el-date-picker v-model="drawerValue.dateRange" v-if="drawerValue.dateType == $t('route.Range')"
            type="daterange" @change="dateChange" />
        </div>
        <div>
          <div>{{ $t('route.Standard_temperature') }}</div>
          <el-radio-group v-model="drawerValue.stdTempType" size="large" @change='stdTempTypeChange'>
            <el-radio-button :label="$t('route.Greater_than_or_equal_to')" />
            <el-radio-button :label="$t('route.Less_than_or_equal_to')" />
            <el-radio-button :label="$t('route.Range')" />
          </el-radio-group>
          <div v-show="drawerValue.stdTempType != $t('route.Range')">
            <el-input type="number" v-model="drawerValue.stdTemp" class="width100px" />
          </div>
          <div v-show="drawerValue.stdTempType == $t('route.Range')">
            <el-input type="number" v-model="query.minStdTemp" />
            <div>-</div>
            <el-input type="number" v-model="query.maxStdTemp" />
          </div>
        </div>

        <div>
          <div>{{ $t('Left') }}</div>
          <el-radio-group v-model="drawerValue.temp4Type" size="large" @change='temp4TypeChange'>
            <el-radio-button :label="$t('route.Greater_than_or_equal_to')" />
            <el-radio-button :label="$t('route.Less_than_or_equal_to')" />
            <el-radio-button :label="$t('route.Range')" />
          </el-radio-group>
          <div v-show="drawerValue.temp4Type != $t('route.Range')">
            <el-input type="number" v-model="drawerValue.temp4" class="width100px" />
          </div>
          <div v-show="drawerValue.temp4Type == $t('route.Range')">
            <el-input type="number" v-model="query.minTemp4" />
            <div>-</div>
            <el-input type="number" v-model="query.maxTemp4" />
          </div>
        </div>

        <div>
          <div>{{ $t('Medium') }}</div>
          <el-radio-group v-model="drawerValue.temp5Type" size="large" @change='temp5TypeChange'>
            <el-radio-button :label="$t('route.Greater_than_or_equal_to')" />
            <el-radio-button :label="$t('route.Less_than_or_equal_to')" />
            <el-radio-button :label="$t('route.Range')" />
          </el-radio-group>
          <div v-show="drawerValue.temp5Type != $t('route.Range')">
            <el-input type="number" v-model="drawerValue.temp5" class="width100px" />
          </div>
          <div v-show="drawerValue.temp5Type == $t('route.Range')">
            <el-input type="number" v-model="query.minTemp5" />
            <div>-</div>
            <el-input type="number" v-model="query.maxTemp5" />
          </div>
        </div>

        <div>
          <div>{{ $t('Right') }}</div>
          <el-radio-group v-model="drawerValue.temp6Type" size="large" @change='temp6TypeChange'>
            <el-radio-button :label="$t('Greater_than_or_equal_to')" />
            <el-radio-button :label="$t('Less_than_or_equal_to')" />
            <el-radio-button :label="$t('Range')" />
          </el-radio-group>
          <div v-show="drawerValue.temp6Type != $t('route.Range')">
            <el-input type="number" v-model="drawerValue.temp6" class="width100px" />
          </div>
          <div v-show="drawerValue.temp6Type == $t('route.Range')">
            <el-input type="number" v-model="query.minTemp6" />
            <div>-</div>
            <el-input type="number" v-model="query.maxTemp6" />
          </div>
        </div>

        <div>
          <el-button size="large" @click="clearClick" type="danger">{{ $t('route.eliminate') }}</el-button>
          <el-button size="large" type="primary" @click='screenSaveClick'>{{ $t('route.sure') }}</el-button>
        </div>
      </div>
    </el-drawer>


    <el-dialog width="500px" v-model="dialogEditVisible">
      <el-form label-width="130px">
        <el-form-item :label="$t('route.Workshop')">
          <el-input class="input" v-model="form.workshop" />
        </el-form-item>
        <el-form-item :label="$t('route.Production_line')">
          <el-input class="input" v-model="form.proLine" />
        </el-form-item>
        <el-form-item :label="$t('route.Standard_temperature')">
          <el-input type="number" class="input" v-model="form.stdTemp" />
        </el-form-item>
        <el-form-item :label="$t('route.Left')">
          <el-input type="number" class="input" v-model="form.temp4" />
        </el-form-item>
        <el-form-item :label="$t('route.Medium')">
          <el-input type="number" class="input" v-model="form.temp5" />
        </el-form-item>
        <el-form-item :label="$t('route.Right')">
          <el-input type="number" class="input" v-model="form.temp6" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click='saveClick'>{{ $t('route.sure') }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>



  </div>
</template>
  
<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import Constant from '../../js/common/constant.js'
import DateUtils from "../../js/common/date-utils";
export default {
  data() {
    return {
      device: null,
      query: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
        kind: null,
        workshop: null,
        startTime: DateUtils.getDayStartDate().format("yyyy-MM-dd hh:mm:ss"),
        endTime: DateUtils.getDayEndDate().format("yyyy-MM-dd hh:mm:ss"),
        minStdTemp: null,
        maxStdTemp: null,
        minTemp4: null,
        maxTemp4: null,
        minTemp5: null,
        maxTemp5: null,
        minTemp6: null,
        maxTemp6: null,
      },
      drawerValue: {
        dateRange: null,
        stdTemp: null,
        temp4: null,
        temp5: null,
        temp6: null,
        dateType: this.$t('route.today'),
        stdTempType: null,
        temp4Type: null,
        temp5Type: null,
        temp6Type: null,
      },
      workshopList: [],
      tableData: [],
      months: ["2022-8", "2022-9", "2022-10", "2022-11"],
      monthday: "2022-11",
      isShowDrawer: false,
      uploadUrl: api.BASE_URI + api.INPUT_DEVICE_DATA1,
      dialogEditVisible: false,
      checkRows: [],
      form: {
        workshop: null,
        proLine: null,
        stdTemp: null,
        temp4: null,
        temp5: null,
        temp6: null,
      }

    };
  },
  methods: {
    deleteClick() {

      this.$confirm(this.$t('route.continue23'), this.$t('route.reminder'), {
        confirmButtonText: this.$t('route.sure'),
        cancelButtonText: this.$t('route.cancel'),
        type: 'warning'
      }).then(() => {
        if (this.checkRows.length == 0) return;
        var idList = [];
        for (var item of this.checkRows) {
          idList.push(item.id);
        }
        http.post(api.DELETE_DEVICE_TEMP_LIST, {
          idList: idList,
        }, data => {
          this.$message.success(this.$t('route.Delete_succeeded'));
          this.getTempList();
        })
      });



    },
    saveClick() {
      var idList = [];
      for (var item of this.checkRows) {
        idList.push(item.id);
      }
      http.post(api.UPDATE_DEVICE_TEMP_LIST, {
        idList: idList,
        workshop: this.form.workshop,
        proLine: this.form.proLine,
        stdTemp: this.form.stdTemp,
        temp4: this.form.temp4,
        temp5: this.form.temp5,
        temp6: this.form.temp6,
      }, data => {
        this.$message.success(this.$t('route.Edit_succeeded'));
        this.getTempList();
        this.dialogEditVisible = false;
      })
    },
    selectionChange(val) {
      this.checkRows = val;
      console.log(JSON.stringify(this.checkRows))
    },
    inputSuccess(res) {
      if (res.resultCode == 0) {
        this.$message.success(this.$t('route.Import_succeeded'));
      } else {
        this.$message.error(res.resultMsg);
      }
    },
    editClick() {
      if (this.checkRows.length == 0) {
        this.$message.error(this.$t('route.Please_select_data_to_edit'));
        return;
      }
      this.dialogEditVisible = true;

    },
    extClick() {
      if (this.drawerValue.stdTempType == this.$t('route.Greater_than_or_equal_to')) {
        this.query.minStdTemp = this.drawerValue.stdTemp;
      } else if (this.drawerValue.stdTempType == this.$t('route.Less_than_or_equal_to')) {
        this.query.maxStdTemp = this.drawerValue.stdTemp;
      }
      if (this.drawerValue.temp4Type == this.$t('route.Greater_than_or_equal_to')) {
        this.query.minTemp4 = this.drawerValue.temp4;
      } else if (this.drawerValue.temp4Type == this.$t('route.Less_than_or_equal_to')) {
        this.query.maxTemp4 = this.drawerValue.temp4;
      }
      if (this.drawerValue.temp5Type == this.$t('route.Greater_than_or_equal_to')) {
        this.query.minTemp5 = this.drawerValue.temp5;
      } else if (this.drawerValue.temp5Type == this.$t('route.Less_than_or_equal_to')) {
        this.query.maxTemp5 = this.drawerValue.temp5;
      }
      if (this.drawerValue.temp6Type == this.$t('route.Greater_than_or_equal_to')) {
        this.query.minTemp6 = this.drawerValue.temp6;
      } else if (this.drawerValue.temp6Type == this.$t('route.Less_than_or_equal_to')) {
        this.query.maxTemp6 = this.drawerValue.temp6;
      }
      var params = {
        deviceId: this.device.id,
        pageNum: this.query.pageNum,
        pageSize: this.query.pageSize,
        workshop: this.query.workshop == this.$t('route.All_workshops') ? null : this.query.workshop,
        startTime: this.query.startTime,
        endTime: this.query.endTime,
        minStdTemp: this.query.minStdTemp,
        maxStdTemp: this.query.maxStdTemp,
        minTemp4: this.query.minTemp4,
        maxTemp4: this.query.maxTemp4,
        minTemp5: this.query.minTemp5,
        maxTemp5: this.query.maxTemp5,
        minTemp6: this.query.minTemp6,
        maxTemp6: this.query.maxTemp6,
      };
      const finalUrl = `${api.BASE_URI + api.EXT_TEMP_PRO2}?${CommonUtils.encodeSearchParams(params)}`
      CommonUtils.download(finalUrl, this.$t('route.Temperature_report') + '.xlsx')
    },
    dateChange(value) {
      this.query.startTime = value[0].format("yyyy-MM-dd hh:mm:ss");
      this.query.endTime = value[1].format("yyyy-MM-dd hh:mm:ss");
    },
    clearClick() {
      this.drawerValue.dateType = this.$t('route.today');
      this.query.startTime = DateUtils.getDayStartDate().format("yyyy-MM-dd hh:mm:ss");
      this.query.endTime = DateUtils.getDayEndDate().format("yyyy-MM-dd hh:mm:ss");
      this.drawerValue.stdTempType = null;
      this.query.minStdTemp = null;
      this.query.maxStdTemp = null;
      this.drawerValue.temp4Type = null;
      this.query.minTemp4 = null;
      this.query.maxTemp4 = null;
      this.drawerValue.temp5Type = null;
      this.query.minTemp5 = null;
      this.query.maxTemp5 = null;
      this.drawerValue.temp6Type = null;
      this.query.minTemp6 = null;
      this.query.maxTemp6 = null;
      this.getTempList();
    },
    screenSaveClick() {
      this.query.pageNum = 1;
      this.getTempList();
      this.isShowDrawer = false;
    },
    handlePageChange(pageNum) {
      this.query.pageNum = pageNum;
      this.getTempList();
    },
    temp4TypeChange() {
      this.query.maxTemp4 = 300;
      this.query.minTemp4 = -20;
      if (this.drawerValue.temp4Type == this.$t('route.Greater_than_or_equal_to')) {
        this.drawerValue.temp4 = -20;
      } else if (this.drawerValue.temp4Type == this.$t('route.Less_than_or_equal_to')) {
        this.drawerValue.temp4 = 300;
      }
    },
    temp5TypeChange() {
      this.query.maxTemp5 = 300;
      this.query.minTemp5 = -20;
      if (this.drawerValue.temp5Type == this.$t('route.Greater_than_or_equal_to')) {
        this.drawerValue.temp5 = -20;
      } else if (this.drawerValue.temp5Type == this.$t('route.Less_than_or_equal_to')) {
        this.drawerValue.temp5 = 300;
      }
    },
    temp6TypeChange() {
      this.query.maxTemp6 = 300;
      this.query.minTemp6 = -20;
      if (this.drawerValue.temp6Type == this.$t('route.Greater_than_or_equal_to')) {
        this.drawerValue.temp6 = -20;
      } else if (this.drawerValue.temp6Type == this.$t('route.Less_than_or_equal_to')) {
        this.drawerValue.temp6 = 300;
      }
    },
    stdTempTypeChange() {
      this.query.maxStdTemp = 300;
      this.query.minStdTemp = -20;
      if (this.drawerValue.stdTempType == this.$t('route.Greater_than_or_equal_to')) {
        this.drawerValue.stdTemp = -20;
      } else if (this.drawerValue.stdTempType == this.$t('route.Less_than_or_equal_to')) {
        this.drawerValue.stdTemp = 300;
      }
    },
    dateTypeChange() {
      if (this.drawerValue.dateType == this.$t('route.today')) {
        this.query.startTime = DateUtils.getDayStartDate().format("yyyy-MM-dd hh:mm:ss");
        this.query.endTime = DateUtils.getDayEndDate().format("yyyy-MM-dd hh:mm:ss");
      } else if (this.drawerValue.dateType == this.$t('route.yesterday')) {
        this.query.startTime = DateUtils.getBeginYesterday().format("yyyy-MM-dd hh:mm:ss");
        this.query.endTime = DateUtils.getEndYesterday().format("yyyy-MM-dd hh:mm:ss");
      } else if (this.drawerValue.dateType == this.$t('route.This_week')) {
        this.query.startTime = DateUtils.getWeekStartDate().format("yyyy-MM-dd hh:mm:ss");
        this.query.endTime = DateUtils.getWeekEndDate().format("yyyy-MM-dd hh:mm:ss");
      } else if (this.drawerValue.dateType == this.$t('route.This_month')) {
        this.query.startTime = DateUtils.getMonthStartDate().format("yyyy-MM-dd hh:mm:ss");
        this.query.endTime = DateUtils.getMonthEndDate().format("yyyy-MM-dd hh:mm:ss");
      } else if (this.drawerValue.dateType == this.$t('route.Last_month')) {
        this.query.startTime = DateUtils.getLastMonthStartDate().format("yyyy-MM-dd hh:mm:ss");
        this.query.endTime = DateUtils.getLastMonthEndDate().format("yyyy-MM-dd hh:mm:ss");
      } else {
        this.query.startTime = DateUtils.getWeekStartDate().format("yyyy-MM-dd hh:mm:ss");
        this.query.endTime = DateUtils.getWeekEndDate().format("yyyy-MM-dd hh:mm:ss");
      }
      this.query.pageNum = 1;
      this.getTempList();
    },
    moreClick() {
      this.isShowDrawer = !this.isShowDrawer;
    },
    getTempList() {
      if (this.drawerValue.stdTempType == this.$t('route.Greater_than_or_equal_to')) {
        this.query.minStdTemp = this.drawerValue.stdTemp;
      } else if (this.drawerValue.stdTempType == this.$t('route.Less_than_or_equal_to')) {
        this.query.maxStdTemp = this.drawerValue.stdTemp;
      }
      if (this.drawerValue.temp4Type == this.$t('route.Greater_than_or_equal_to')) {
        this.query.minTemp4 = this.drawerValue.temp4;
      } else if (this.drawerValue.temp4Type == this.$t('route.Less_than_or_equal_to')) {
        this.query.maxTemp4 = this.drawerValue.temp4;
      }
      if (this.drawerValue.temp5Type == this.$t('route.Greater_than_or_equal_to')) {
        this.query.minTemp5 = this.drawerValue.temp5;
      } else if (this.drawerValue.temp5Type == this.$t('route.Less_than_or_equal_to')) {
        this.query.maxTemp5 = this.drawerValue.temp5;
      }
      if (this.drawerValue.temp6Type == this.$t('route.Greater_than_or_equal_to')) {
        this.query.minTemp6 = this.drawerValue.temp6;
      } else if (this.drawerValue.temp6Type == this.$t('route.Less_than_or_equal_to')) {
        this.query.maxTemp6 = this.drawerValue.temp6;
      }
      http.post(
        api.GET_TEMP_LIST2, {
        deviceId: this.device.id,
        pageNum: this.query.pageNum,
        pageSize: this.query.pageSize,
        workshop: this.query.workshop == this.$t('route.All_workshops') ? null : this.query.workshop,
        startTime: this.query.startTime,
        endTime: this.query.endTime,
        minStdTemp: this.query.minStdTemp,
        maxStdTemp: this.query.maxStdTemp,
        minTemp4: this.query.minTemp4,
        maxTemp4: this.query.maxTemp4,
        minTemp5: this.query.minTemp5,
        maxTemp5: this.query.maxTemp5,
        minTemp6: this.query.minTemp6,
        maxTemp6: this.query.maxTemp6,
      }, data => {
        this.tableData = data.data.tempList;
        this.query.total = data.data.total;
      }
      );
    },
    getAllWorkshop() {
      http.post(api.GET_ALL_WORKSHOP_NAME, {
        adminId: this.$store.state.user.id,
        factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
      }, data => {
        this.workshopList = data.data;
        this.workshopList.unshift(this.$t('route.All_workshops'));
      });
    },
    format1(row) {
      return new Date(row.gmtCreate).format('yyyy-MM-dd');
    },
    format2(row) {
      return new Date(row.gmtCreate).format('hh:mm:ss');
    },
    format3() {
      return this.device.noseCode;
    },
    format4() {
      return this.device.kind;
    },
    format5(row, column, cellValue, index) {
      // return Math.round(cellValue);
      return cellValue;
    },


  },
  created() {
    this.device = JSON.parse(CommonUtils.getParam(this, "device", "setDevice"));
    this.getAllWorkshop();
    // this.getTempList();

  },

};
</script>
  
 
<style lang="scss" scoped>
.temp-report {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  >div {
    // display: flex;
    margin-top: 10px;
    background-color: #ffffff;
    padding: 10px;
    border-radius: 5px;
  }

  >div:nth-of-type(1) {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    >label:nth-of-type(1) {
      font-weight: 600;
    }

    >div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    >button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      >img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }
  }

  >div:nth-of-type(2) {
    display: flex;
    flex-direction: row;
    align-items: center;

  }


  >div:nth-of-type(3) {
    display: flex;
    flex-direction: column;

    >div:nth-of-type(2) {
      margin-top: 10px;
      display: flex;
      justify-content: flex-end;
      // background-color: #f00;
    }
  }

  .drawer {
    >div {
      margin-bottom: 20px;

      >div {
        margin-bottom: 10px;
      }
    }

    >div:nth-of-type(2),
    >div:nth-of-type(3),
    >div:nth-of-type(4),
    >div:nth-of-type(5) {
      >div:nth-of-type(4) {
        display: flex;
        flex-direction: row;

        >div {
          text-align: center;
          width: 100px;
        }

        >div:nth-of-type(2) {
          width: 30px;
        }
      }
    }

    >div:last-of-type {
      display: flex;
      flex-direction: row;

      >button {
        flex: 1;
      }
    }
  }

  .div-handle-right {
    flex: 1;
    flex-direction: row-reverse;
    text-align: right;
    display: flex;
    // margin-top: 10px;
    // background-color: #f00;
  }

  .input {
    width: 300px;
  }
}
</style>